<?php
namespace app\index\controller;
use \think\Controller;
use \think\Db;
class Index extends Controller
{
    public function index()
    {
    	// $data = Db::query('select post.*,user.avatar,user.username,user.level,user.is_approve,cate.cname from post left join user on post.uid = user.uid LEFT JOIN cate ON post.class_id = cate.cid');
    	// $data['ctime']=ftime($data['ctime']);
        // 综合贴
    	$data = Db::table('post')
    	->alias('p')
    	->join('user u','p.uid = u.uid')
    	->join('cate c','p.class_id = c.cid')
    	->field('post.*,cate.cname,user.avatar,user.username,user.level,user.is_approve')
    	->order('ctime desc')
    	->select();

        //置顶贴
        $top = Db::table('post')
        ->alias('p')
        ->join('user u','p.uid = u.uid')
        ->join('cate c','p.class_id = c.cid')
        ->field('post.*,cate.cname,user.avatar,user.username,user.level,user.is_approve')
        ->where('p.is_top','=','1')
        ->order('ctime desc')
        ->select();

        //本周热议
        $hotPost = Db::table('post')->field('pid,answer_num,title')->order('answer_num desc')->limit(10)->select();

        //回帖周榜
        $replyRank = Db::table('reply t1')
            ->field('t2.username,t2.avatar,t2.uid, count(t1.rid) cn')
            ->join('user t2','t1.uid=t2.uid')
            ->where('t1.ctime','>',strtotime('-7 days'))
            ->group('t1.uid')
            ->order('cn desc')
            ->limit(15)
            ->select();

        $cate = Db::table('cate')->select();

        //签到数据
        $sign = Db::table('sign')->where(['uid'=>session('uid'),'date'=>date('Y-m-d')])->find();
        return $this->fetch('',['replyRank'=>$replyRank,'data'=>$data,'top'=>$top,'sign'=>$sign,'hotPost'=>$hotPost,'cate'=>$cate]);
       
    }
  
}
